package handle

import (
	"io/ioutil"
	"os"
	"strings"

	"MPCBot/global"
	"MPCBot/model"
	"MPCBot/utils"
)

var (
	hermitClient HermitClient
)

type ChannelI[V int] chan V

// system Initialize
func Init() {
	defer func() {
		if err := recover(); err != nil {
			global.Log.Error("service Initialization fail：%v", err)
		}
	}()

	// Generate robot accountlist
	global.Log.Info("====> service Initializing ......")
	switch global.Conf.RunParams.ServiceType {
	case model.ExecCrossChainTask:
		mnemonic, err := ioutil.ReadFile(global.Conf.RunParams.MnemonicPath)
		if err != nil {
			panic(err)
		}
		hermitClient.ExportAccountPrivate(string(mnemonic))
	case model.ExportAccountPrivate:
		var password string
		words, _ := utils.GetPasswdPrompt("Please enter the mnemonic password :", false, os.Stdin, os.Stdout)
		password += " " + strings.ToLower(string(words))
		mnemonic, err := ioutil.ReadFile(global.Conf.RunParams.MnemonicPath)
		if err != nil {
			panic(err)
		}
		hermitClient.Init(password, string(mnemonic))

		bankAdrList := []string{
			"ghm1ngezdepsfqtukwtyd72fadytt4a7cv8vlh98g2",
			"ghm1ael9q4prnu6gtz7jer28r6j5p4fkd9xfaf2g0s",
		}
	
		tokenAdrList := []string{
			"ghm1gd4wse7v6rq6myptxuv9mpgq8gqy85yw6dcym4",
			"ghm19f3vzxucvtjrv7venq8x3w0vqsyztc6n97jj97",
			"ghm1hqj3tnclwstk827mw8le9gxuq2rrcjpf0vpjg4",
		}

		err = hermitClient.queryTransferHistoryMsg(tokenAdrList[0], "", uint32(0), uint32(100))
		if err != nil {
			global.Log.Error("====> errinfo %v", err.Error())
		}
		
		for _, bankAdr := range bankAdrList {
			for _, tokenAdr := range tokenAdrList {
				hermitClient.ExecuteTask(bankAdr, tokenAdr)
			}
		}
	}
	global.Log.Info("====> service Initialization complete")
}
